/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.vraptor.jpessoal.DAO;

import br.com.caelum.vraptor.ioc.Component;
import br.com.vraptor.jpessoal.bean.Setor;
import br.com.vraptor.jpessoal.util.HibernateUtil;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

@Component
public class SetorDAO {
    
    public List<Setor> listar(){
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Query query = session.createQuery("from Setor");
        List<Setor> lista = query.list();
        session.getTransaction().commit();
                
        return lista;        
    }
    
    public List<Setor> listar(String where){
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Query query = session.createQuery("from Setor WHERE :where");
        query.setParameter("where", where);
        List<Setor> lista = query.list();
        session.getTransaction().commit();
                
        return lista;   
    }
    
    public Setor obterPorId(Integer id){
        
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Query query = session.createQuery("from Setor where id = :id ");
        query.setParameter("id", id);
        Setor listar = (Setor) query.uniqueResult();
        session.getTransaction().commit();
                
        return listar;
    }
    
    public void save(Setor setor){
        
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.save(setor);
        session.getTransaction().commit();
        
    }
      public void atualizar(Setor setor){
         
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.update(setor);
        session.getTransaction().commit();
         
     }
    
}
